package com.example.service.impl;

import com.example.mapper.DeptMapper;
import com.example.mapper.EmpMapper;
import com.example.pojo.Dept;
import com.example.pojo.Emp;
import com.example.service.DeptService;
import com.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    public List<Dept> getAll() {
        System.out.println(deptMapper.getAll());
        return deptMapper.getAll();
    }

    public boolean deleteDeptById(Integer id) {
        List<Emp> emps = empMapper.getEmpsByDeptId(id);
        if(!emps.isEmpty()) return true;
        return false;
    }

    public boolean addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.addDept(dept) > 0;
    }

    public Dept getDeptById(Integer id) {
        return deptMapper.getDeptById(id);
    }

    public boolean updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.update(dept) > 0;
    }
}
